package com.weiyan.server.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.weiyan.config.BlockHandler;
import com.weiyan.config.Fallback;
import com.weiyan.dao.OrderDao;
import com.weiyan.domian.Order;
import com.weiyan.fegin.ProductServer;
import com.weiyan.server.OrderServer;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author misterWei
 * @create 2020年02月20号:20点20分
 * @mailbox mynameisweiyan@gmail.com
 */
@Service
@Slf4j
public class OrderServerImpl implements OrderServer {
    @Autowired
    private OrderDao orderDao;
/*
    @Autowired
    private ProductServer productServer;*/


    @Reference // 引用 dubbo 的引用
    private com.weiyan.service.ProductService productServer;

    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    @Transactional(rollbackFor = Exception.class)
    public void save(Order order) {
          //减库存
        orderDao.save(order);
        productServer.decrement(order.getPid());
        log.error(RootContext.getXID());
    }
    int i = 0;
    @Override
    @SentinelResource(value = "message",
            blockHandler = "message",//指定的方法名称
            blockHandlerClass = BlockHandler.class,
            fallback = "message", //指定的方法名称
            fallbackClass = Fallback.class

    )
    public void message() {
         i ++;
         if (i %3 ==0){
             throw new RuntimeException("自定义异常捕获...");
         }
        log.info("{}","调用了...");
    }

}
